import workerpool from "workerpool"

import { fullOrderToProject } from "./component/fullOrderToProject.js"
import { taskProcess } from "./component/taskProcess.js"
import { colorDiff } from "./component/colorDiff.js"
import { syncKingdeeOrder } from "./component/syncKingdeeOrder.js"
import { kingdeeStartWorking } from "./component/kingdeeStartWorking.js"

import { getOrderId, getProductionOrderList } from "./component/util.js"

import { getStorageKey, localStorage, StorageKeyType } from "./component/localStorage.js"

import reportList from './asset/report.json'     with { type: "json" }
import oldReportList from './asset/oldReportList.json'     with { type: "json" }

const orderStatus = {
    empty: 0,
    fullOrder: 1,
    colorDiff: 2,
    syncKingdeeOrder: 3,
    kingdeeStartWorking: 4,
    taskProcess: 5
}

async function run(orderCode, oldOrderCode) {
    const orderId = await getOrderId(orderCode)

    const storageKey = getStorageKey(orderId, StorageKeyType.mainStatus)
    const currStatus = localStorage.getItem(storageKey) || orderStatus.empty

    if (currStatus < orderStatus.fullOrder) {
        await fullOrderToProject(orderCode, orderId)
        localStorage.setItem(storageKey, orderStatus.fullOrder)
    }

    let runProductionOrderList = await getRunProductionOrder(orderId, true)
    // 如果双方都入库就不需要执行以下内容
    // runProductionOrderList = runProductionOrderList.filter(productionOrder => {
    //     const oldCode = productionOrder.oldCode
    //     const itemNum = productionOrder.totalNum
    //     const hasOldStockIn = oldReportList.some(item => item['生产批号'] == oldCode && item.操作类型 === '入库' && parseInt(item.完成数量) === itemNum)
    //     const hasNewStockIn = reportList.some(item => item['生产批号'] == oldCode && item.操作类型 === '入库' && parseInt(item.完成数量) === itemNum)
    //     return !(hasOldStockIn && hasNewStockIn)
    // })
    if (runProductionOrderList.length === 0) {
        return orderCode
    }
    if (currStatus < orderStatus.colorDiff) {
        await colorDiff(orderCode, runProductionOrderList)
        localStorage.setItem(storageKey, orderStatus.colorDiff)
    }

    runProductionOrderList = runProductionOrderList.filter(item => item.deliverType === 1)
    if (currStatus < orderStatus.syncKingdeeOrder) {
        await syncKingdeeOrder(orderCode, runProductionOrderList)
        localStorage.setItem(storageKey, orderStatus.syncKingdeeOrder)
    }


    runProductionOrderList = await getRunProductionOrder(orderId, true)
    runProductionOrderList = runProductionOrderList.filter(item => item.deliverType === 1)
    // 新的有入库旧的没入库就执行第三步报工
    // runProductionOrderList = runProductionOrderList.filter(item => {
    //     const notFinish = item.status !== 1
    //     const oldCode = item.oldCode
    //     const itemNum = item.totalNum
    //     const hasOldStockIn = oldReportList.some(item => item['生产批号'] == oldCode && item.操作类型 === '入库' && parseInt(item.完成数量) === itemNum)
    //     const hasNewStockIn = reportList.some(item => item['生产批号'] == oldCode && item.操作类型 === '入库' && parseInt(item.完成数量) === itemNum)
    //     return notFinish && (hasNewStockIn && !hasOldStockIn)
    // })
    if (runProductionOrderList.length === 0) {
        return orderCode
    }
    if (currStatus < orderStatus.kingdeeStartWorking) {
        await kingdeeStartWorking(orderCode, runProductionOrderList)
        localStorage.setItem(storageKey, orderStatus.kingdeeStartWorking)
    }
    // runProductionOrderList = runProductionOrderList.filter(productionOrder => {
    //     const oldCode = productionOrder.oldCode
    //     const totalNum = productionOrder.totalNum
    //     const hasFinishing = reportList.some(item => item['生产批号'] == oldCode && item.操作类型 === '后整理' && parseInt(item.完成数量) === totalNum)
    //     const stockIn = reportList.find(item => item['生产批号'] == oldCode && item.操作类型 === '入库')
    //     return hasFinishing && (!stockIn || parseInt(stockIn.完成数量) === 0)
    // })
    // if (runProductionOrderList.length === 0) {
    //     return orderCode
    // }
    // if (currStatus < orderStatus.taskProcess) {
    //     await taskProcess(orderCode, runProductionOrderList)
    //     localStorage.setItem(storageKey, orderStatus.taskProcess)
    // }

    return orderCode
}

async function getRunProductionOrder(orderId, igonoreFinish) {
    let productionOrderList = await getProductionOrderList(orderId)
    return productionOrderList.filter(item => {
        const oldCode = item.oldCode
        const totalNum = item.totalNum
        const stockIn = reportList.some(item => item['生产批号'] == oldCode && item.操作类型 === '入库')
        return item.styleCode[0] === 'K'
            && [1, 3].includes(item.deliverType)
            && item.oldCode != null
            && (igonoreFinish || item.status !== 1)
            && parseInt(stockIn?.完成数量) !== totalNum
    })
}

async function test(orderCode, oldOrderCode) {
    await new Promise(res => setTimeout(res, 5000))
    return orderCode
}

// create a worker and register public functions
workerpool.worker({
    run: run,
    test: test
})
